package com.mti.epivisitemobile.view
{
	import com.mti.epivisite.common.event.EventProxyBase;
	import com.mti.epivisite.common.map.MapImages;
	import com.mti.epivisitemobile.Constants;
	
	import flash.events.MouseEvent;
	import flash.globalization.DateTimeStyle;
	
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import spark.formatters.DateTimeFormatter;
	
	public class EventViewMediator extends Mediator
	{
		public static const NAME:String = "EventViewMediator";
		
		public function EventViewMediator(viewComponent:EventView)
		{
			super(NAME, viewComponent);
			
			view.addEventListener(FlexEvent.CREATION_COMPLETE, onViewComplete);
		}
		
		private var proxy:EventProxyBase = null;
		
		private function onViewComplete(event:FlexEvent):void
		{
			view.removeEventListener(FlexEvent.CREATION_COMPLETE, onViewComplete);
			
			proxy = EventProxyBase(facade.retrieveProxy(EventProxyBase.NAME));
			if (proxy.isLoaded == true)
			{
				updateView();
			}
			view.mapImage.addEventListener(MouseEvent.CLICK, onMapClick)
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				EventProxyBase.LOAD_SUCCESS
			];
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch (notification.getName())
			{
				case EventProxyBase.LOAD_SUCCESS:
					updateView();
					break;
			}
		}
		
		private function updateView():void
		{
			view.titleLabel.text = proxy.baseEvent.name;
			var dateFormatter:DateTimeFormatter = new DateTimeFormatter();
			dateFormatter.dateStyle = DateTimeStyle.LONG;
			dateFormatter.timeStyle = DateTimeStyle.NONE;
			view.dateLabel.text = dateFormatter.format(proxy.baseEvent.startDate);
			if (proxy.baseEvent.startDate != proxy.baseEvent.endDate)
				view.dateLabel.text += " - " + dateFormatter.format(proxy.baseEvent.endDate);
			view.descriptionLabel.text = proxy.baseEvent.description;
			view.mapImage.source = MapImages.Plan;
		}
		
		private function onMapClick(event:MouseEvent):void
		{
			sendNotification(Constants.SHOW_MAP);
		}
		
		public function get view():EventView
		{
			return EventView(viewComponent);
		}
	}
}